package com.banciyuan.bcywebview.biz.main.newdiscover.index.discoverholder;

import android.content.Context;
import android.text.TextUtils;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.banciyuan.bcywebview.R;
import com.banciyuan.bcywebview.base.helper.timelinedatacenter.DetailDataHelper;
import com.banciyuan.bcywebview.base.view.textview.EllipsezeTextView;
import com.banciyuan.bcywebview.utils.common.UIUtils;
import com.banciyuan.bcywebview.utils.universalimageloader.core.ImageLoader;
import com.banciyuan.bcywebview.utils.universalimageloader.utils.ImageOptions;

import de.greenrobot.daoexample.model.Timeline;

/**
 * Created by squall on 2017/3/22.
 */

public class DailyDiscoverHolder extends BaseDiscoverHolder {

    private ImageView[] content;
    private View imagePics;
    private TextView pagenum;
    private EllipsezeTextView intro;

    public DailyDiscoverHolder(View itemView) {
        super(itemView);
        imagePics = itemView.findViewById(R.id.ground_content_rly);
        content = new ImageView[]{
                (ImageView) itemView.findViewById(R.id.ground_content_img_one),
                (ImageView) itemView.findViewById(R.id.ground_content_img_two),
                (ImageView) itemView.findViewById(R.id.ground_content_img_three)
        };
        pagenum = (TextView) itemView.findViewById(R.id.ground_pagenum_tv);
        intro = (EllipsezeTextView) itemView.findViewById(R.id.ground_intro_tv);
    }

    @Override
    public void renderData(final Timeline mItem, final Context context) {
        super.renderData(mItem, context);

        if (!TextUtils.isEmpty(mItem.getContentAddress())) {
            String[] addressList = mItem.getContentAddress().split(" ");
            int dailyLength = addressList.length;
            if (dailyLength == 0) {
                imagePics.setVisibility(View.GONE);
            } else {
                imagePics.setVisibility(View.VISIBLE);
                for (int i = 0; i < content.length; i++) {
                    LinearLayout.LayoutParams ll = new LinearLayout.LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT);
                    if (dailyLength == 1) {
                        ll.weight = 2 - i;
                    } else {
                        ll.weight = 1;
                        if (i < dailyLength - 1) {
                            ll.rightMargin = UIUtils.dip2px(4, context);
                        } else {
                            ll.rightMargin = 0;
                        }
                    }
                    content[i].setLayoutParams(ll);
                    if (i < dailyLength) {
                        content[i].setVisibility(View.VISIBLE);
                        ImageLoader.getInstance().displayImage(addressList[i], content[i], ImageOptions.options);
                    } else {
                        if (dailyLength == 1) {
                            content[i].setVisibility(View.INVISIBLE);
                        } else {
                            content[i].setVisibility(View.GONE);
                        }
                    }
                }
            }
        } else {
            imagePics.setVisibility(View.GONE);
        }
        String pic_num = mItem.getPic_num();
        if (!TextUtils.isEmpty(pic_num)) {
            if (Integer.parseInt(pic_num) > 3) {
                pagenum.setVisibility(View.VISIBLE);
            } else {
                pagenum.setVisibility(View.GONE);
            }
        } else {
            pagenum.setVisibility(View.GONE);
        }
        setRecommendIntro(context, intro, mItem, mItem.getIntro(), context.getString(R.string.write_name));
        intro.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setDiscoverViewClick(mItem, context);
            }
        });
        itemView.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                setDiscoverViewClick(mItem, context);
            }
        });
    }

    private void setRecommendIntro(Context mContext, EllipsezeTextView tvIntro, Timeline mItem, String intro, String defaultIntro) {
        String finalIntro = TextUtils.isEmpty(intro) ? defaultIntro : intro;
        finalIntro = String.format(mContext.getResources().getString(R.string.card_recommended_user_format), mItem.getOUName(), finalIntro);
        tvIntro.setText(finalIntro, mItem.getOUName(), mItem.getOuid(), true);
        tvIntro.setVisibility(View.VISIBLE);
    }

    private void setDiscoverViewClick(Timeline mTimeline, Context context) {
        if (null != mTimeline) {
            DetailDataHelper.gotoDailyDetail(context, mTimeline.getUd_id(), false);
        }
    }
}
